/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.component;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

class _ComponentFacetMap
        implements Map<String, UIComponent>, Serializable {
    private static final long serialVersionUID = -3456937594422167629L;
    private UIComponent _component;
    private Map<String, UIComponent> _map = new HashMap<String, UIComponent>();

    _ComponentFacetMap(UIComponent component) {
        _component = component;
    }

    private void checkKey(Object key) {
        if (key == null) {
            throw new NullPointerException("key");
        }
        if (!(key instanceof String)) {
            throw new ClassCastException("key is not a String");
        }
    }

    private void checkValue(Object value) {
        if (value == null) {
            throw new NullPointerException("value");
        }
        if (!(value instanceof UIComponent)) {
            throw new ClassCastException("value is not a UIComponent");
        }
    }

    public void clear() {
        _map.clear();
    }

    public boolean containsKey(Object key) {
        checkKey(key);
        return _map.containsKey(key);
    }

    public boolean containsValue(Object value) {
        checkValue(value);
        return _map.containsValue(value);
    }

    public Set<Entry<String, UIComponent>> entrySet() {
        return _map.entrySet();
    }

    public UIComponent get(Object key) {
        checkKey(key);
        return _map.get(key);
    }

    public boolean isEmpty() {
        return _map.isEmpty();
    }

    public Set<String> keySet() {
        return _map.keySet();
    }

    public UIComponent put(String key, UIComponent value) {
        checkKey(key);
        checkValue(value);
        setNewParent(key, value);
        return _map.put(key, value);
    }

    public void putAll(Map<? extends String, ? extends UIComponent> m) {
        for (Iterator<? extends Map.Entry<? extends String, ? extends UIComponent>> i = m.entrySet().iterator(); i
                .hasNext();) {
            Map.Entry<? extends String, ? extends UIComponent> e = i.next();
            put(e.getKey(), e.getValue());
        }
    }

    public UIComponent remove(Object key) {
        checkKey(key);
        UIComponent facet = _map.remove(key);
        if (facet != null) {
            facet.setParent(null);
        }
        return facet;
    }

    private void setNewParent(String facetName, UIComponent facet) {
        UIComponent oldParent = facet.getParent();
        if (oldParent != null) {
            oldParent.getFacets().remove(facetName);
        }
        facet.setParent(_component);
    }

    public int size() {
        return _map.size();
    }

    public Collection<UIComponent> values() {
        return _map.values();
    }

}
